#include "rtc_board.h"

volatile bool RtcTiggerFlag = false;
#if 0
__INTERRUPT
__HIGH_CODE
void RTC_IRQHandler(void)
{
  R8_RTC_FLAG_CTRL = (RB_RTC_TMR_CLR | RB_RTC_TRIG_CLR);
  RtcTiggerFlag = true;
}
#endif
void LibraryCalibrationLSI(void)
{
  Calibration_LSI(Level_64);
}

bool RtcGetIrqnTiggerEn(void)
{
  return RtcTiggerFlag;
}

void RtcSetTiggerTime(uint32_t Time)
{
  RtcTiggerFlag = false;

  sys_safe_access_enable();
  R32_RTC_TRIG = Time;
  sys_safe_access_disable();
}

void RtcHardwareInitialization(void)
{
#if (CLK_OSC32K)
  sys_safe_access_enable();
  R8_CK32K_CONFIG &= ~(RB_CLK_OSC32K_XT | RB_CLK_XT32K_PON);
  sys_safe_access_disable();
  sys_safe_access_enable();
  R8_CK32K_CONFIG |= RB_CLK_INT32K_PON;
  sys_safe_access_disable();
  LSECFG_Current(LSE_RCur_100);
  LibraryCalibrationLSI();
#else
  // LSECFG_Capacitance(LSECap_27p);
  // LSECFG_Current(LSE_RCur_200);

  sys_safe_access_enable();
  R8_CK32K_CONFIG |= RB_CLK_OSC32K_XT | RB_CLK_INT32K_PON | RB_CLK_XT32K_PON;
  sys_safe_access_disable();
#endif

  RTC_InitTime(2020, 1, 1, 0, 0, 0);
  TMOS_TimerInit(0);
}
